Skip to content

Fix: show whitespace escape codes in string diffs (issue #10704) - #15282

Open
gabrielefigueiredo wants to merge 4 commits into
pytest-dev:mainfrom
gabrielefigueiredo:issue-10704-whitespace-escape
Open

gabrielefigueiredo wants to merge 4 commits into
pytest-dev:mainfrom
gabrielefigueiredo:issue-10704-whitespace-escape

Conversation

@gabrielefigueiredo

Copy link
Copy Markdown

Summary of Changes

Fixes #10704 by displaying explicit whitespace escape codes (\t, \r, \v, \f, \u00a0) in text comparison diffs within src/_pytest/assertion/compare_text.py, while preserving structural newlines (\n).

Context & Motivation

When an assertion comparing two strings failed due to non-printable or hidden whitespace characters (such as tab characters or non-breaking spaces), the assertion failure diff printed them as ordinary single spaces. This made it difficult for developers to diagnose why two visually identical strings failed the assertion.

Implementation Details

  • Intra-line whitespace mapping (_WHITESPACE_MAP & _escape_whitespace): Added a mapping dictionary and helper function in src/_pytest/assertion/compare_text.py that translates invisible intra-line whitespace characters (\t, \r, \v, \f, \u00a0) into explicit escape string representations (e.g., r"\t").

  • Newline (\n) handling: Intentionally excluded \n from the whitespace translation map before ndiff. Replacing \n with r"\n" would flatten multi-line strings into a single line, destroying readable line-by-line diffs. \n continues to be handled via:

    1. Structural line splitting (splitlines()) and ndiff line additions/deletions.
    2. Expressive string representation in the AssertionError summary line via repr().
    3. Global repr() escaping when strings contain only whitespace (isspace()).
  • Caret alignment (? ^): Preserved the visual alignment of ndiff caret markers.


Terminal Output Examples

Scenario 1: Intra-line Escape Code (e.g., Tab \t vs. Space)

Comparing actual = "hello\tworld" with expected = "hello world" explicitly exposes \t in the diff output:

E   AssertionError: assert 'hello\tworld' == 'hello world'
E     - hello world
E     ?      ^
E     + hello\tworld
E     ?      ^^

Scenario 2: Structural Newline (\n vs. Space)

Comparing actual = "hello\nworld" with expected = "hello world" displays \n in the assertion summary header while rendering a clean multi-line structural diff:

E   AssertionError: assert 'hello\nworld' == 'hello world'
E     - hello world
E     + hello
E     + world

Testing & Verification

  • Unit tests: Updated testing/test_assertion.py with parametrized unit tests using callequal, covering \t, \r, \v, \f, \u00a0, trailing whitespace, and identical string cases.
  • Terminal integration test: Added test_whitespace_diff_pytester, utilizing the pytester fixture to verify that terminal outputs explicitly render whitespace escape codes.
  • Regression check: Full assertion test suite passes without regressions (217/217 tests in testing/test_assertion.py).
  • Changelog: Added changelog/10704.bugfix.rst fragment.

Checklist

  • Full test suite passes (pytest testing/test_assertion.py)
  • Added changelog fragment in changelog/10704.bugfix.rst
  • Handled all non-printable intra-line whitespace characters
  • Preserved multi-line diff formatting and ndiff caret alignment

@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 28, 2026
@gabrielefigueiredo

Copy link
Copy Markdown
Author

Good morning! I’ve been checking on my contributions and noticed that I haven’t received any feedback on my open PR yet. Could you please take a look when you have a chance? I’d really appreciate it. Thank you!

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on pytest ! I think it make sense. I didn't comment on all the spanish and all the churn, please fix all the occurrences anyway :)

Comment thread src/_pytest/assertion/compare_text.py Resolved
Comment thread src/_pytest/assertion/compare_text.py Resolved
Comment thread src/_pytest/assertion/compare_text.py Resolved
gabrielefigueiredo and others added 2 commits December 4, 2026 18:57
git add src/_pytest/assertion/compare_text.py
git rebase --continuegit add src/_pytest/assertion/compare_text.py
git rebase --contin

@gabrielefigueiredo gabrielefigueiredo left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review, @Pierre-Sassoulas! I've applied the requested changes and updated the branch. Ready for another look whenever you have a moment.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show whitespace escape code in string diffs

2 participants